package comprehensive;

import java.util.ArrayList;
import java.util.List;

public class Exercises3 {
    // 17、电话号码的字母组合
    String[] str = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    List<String> ret;
    StringBuilder path; // 使用 StringBuilder 进行字符之间的拼接组成字符串

    public List<String> letterCombinations(String digits) {
        ret = new ArrayList<>();
        path = new StringBuilder();

        if(digits.length() == 0) {
            return ret;
        }

        dfs(digits,0);

        return ret;
    }

    public void dfs(String digits,int pos) {

        if (pos == digits.length()) {
            ret.add(path.toString());
            return;
        }

        // 得到 digits 的数字进而得到str对应下标的字符
        // 我们的pos的值就是digits我们需要的位置的值
        String cur = str[digits.charAt(pos) - '0'];
        for(int i = 0;i < cur.length();i++) {
            path.append(cur.charAt(i));
            dfs(digits,pos + 1);
            path.deleteCharAt(path.length() - 1);
        }
    }
}
